﻿/*
 * BFind - A tool for finding football matches for trading on Betfair
 * Copyright 2009, 2010 Magnus Olsson <magnus72(at)gmail.com>
 * 
 * This file is part of BFind.
 * 
 * BFind is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BFind is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BFind.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Collections.Generic;

namespace betfairfind.Rules
{
    public class U25START : BaseRule
    {
        private double UNDER25_MIN_ODDS = 1.75;
        private double UNDER25_MAX_ODDS = 2.10;

        private double MATCHODDS_FAV_TEAM_MIN_ODDS = 1.5;
        //double FAV_TEAM_MAX_ODDS = 1;

        private double OU25_MIN_MATCHED = 10000;
        private double MATCHODDS_MIN_MATCHED = 50000;


        public U25START(Dictionary<string, string> config, List<MyMarket> inplaymarkets, Dictionary<long, MarketPricesCompressed> marketprices)
            : base()
        {

            MarketsUsedInside = new string[] { "match odds", "over/under 2.5 goals" };

            try
            {

                UNDER25_MIN_ODDS = Convert.ToDouble(config["Rule.U25START.UNDER25_MIN_ODDS"]);
                UNDER25_MAX_ODDS = Convert.ToDouble(config["Rule.U25START.UNDER25_MAX_ODDS"]);
                MATCHODDS_FAV_TEAM_MIN_ODDS = Convert.ToDouble(config["Rule.U25START.MATCHODDS_FAV_TEAM_MIN_ODDS"]);
                OU25_MIN_MATCHED = Convert.ToDouble(config["Rule.U25START.OU25_MIN_MATCHED"]);
                MATCHODDS_MIN_MATCHED = Convert.ToDouble(config["Rule.U25START.MATCHODDS_MIN_MATCHED"]);
            }
            catch (Exception e)
            {
                Logger.LogError(e.ToString(), true, true);
            }


            Logger.Log("============================================================================", true, true);
            Logger.Log("Matches qualified for trading U2.5 based on odds " + UNDER25_MIN_ODDS + " to " + UNDER25_MAX_ODDS, true, true);
            Logger.Log("----------------------------------------------------------------------------", true, true);

            if (!CheckRequiredMarkets()) { return; }


            string[] games = FindSoccerGames.GetMatches();

            int selectionCount = 0;
            foreach (var game in games)
            {
                try
                {
                    MyMarket market = FindSoccerGames.GetMarket(game, "Match odds");
                    MarketPricesCompressed mpc_matchodds = marketprices[FindSoccerGames.GetMarket(game, "Match odds").marketId];
                    MarketPricesCompressed mpc_ou25 = marketprices[FindSoccerGames.GetMarket(game, "Over/Under 2.5 goals").marketId];

                    double u25 = mpc_ou25.GetBestBackOdds(0);
                    
                    double fav = 0;
                    if (mpc_matchodds.GetBestBackOdds(0) < mpc_matchodds.GetBestBackOdds(1))
                    {
                        fav = mpc_matchodds.GetBestBackOdds(0);

                    }
                    else
                    {
                        fav = mpc_matchodds.GetBestBackOdds(1);
                    }

                    double mo_matched = mpc_matchodds.TotalMatched;
                    double matched_ou25 = mpc_ou25.TotalMatched;

                    if (fav > MATCHODDS_FAV_TEAM_MIN_ODDS && 
                        (u25 > UNDER25_MIN_ODDS && u25 < UNDER25_MAX_ODDS) &&
                        (mo_matched >= MATCHODDS_MIN_MATCHED && matched_ou25 >= OU25_MIN_MATCHED))
                    {
                        Logger.Log(market.marketTime + ", " + BF.convertPathToGame(game) + ", odds for u2.5: " + u25 + ", matched on O/U 2.5 " + config["CURRENCY_SIGN"] + matched_ou25 + "  -- Fav: " + fav, true, true);
                        selectionCount++;
                    }
                }
                catch (Exception ex)
                {

                    Logger.LogError("Error in game " + game + Environment.NewLine + ex.ToString(), true, false);
                }


            }
            if (selectionCount == 0)
            {
                Logger.Log("No matches", true, true);
            }

        }

    }
}
